package so.wy.servicecenter.businessbed.manager.normal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.frame.manager.AbstractManager;
import so.wy.servicecenter.businessbed.dao.enums.StatusOrderPayEnum;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital.HospitalDAO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital.HospitalDO;
import so.wy.servicecenter.businessbed.dao.rdb.order.order.OrderDAO;
import so.wy.servicecenter.businessbed.dao.rdb.order.order.OrderDO;

@Service
@Scope("prototype")
public class OrderOptNManager extends AbstractManager {
    @Autowired
    private OrderDAO orderDAO;

    @Autowired
    private HospitalDAO hospitalDAO;

    //订单支付结果
    public void handOrderPayResult(boolean paySuccess,String orderSerial){
        OrderDO orderDO = orderDAO.selectOne(new LambdaQueryWrapper<OrderDO>().eq(OrderDO::getOrderSerial, orderSerial));

        //更新订单支付信息
        orderDO.setPayStatus(StatusOrderPayEnum.ofPayResult(paySuccess).getCode());

        //如果支付成功则更新对应医院信息
        if (paySuccess){
            HospitalDO hospitalDO = hospitalDAO.selectById(orderDO.getHospitalId());
            hospitalDO.setAmountPay(hospitalDO.getAmountPay().add(orderDO.getOrderAmount()));
            hospitalDAO.updateById(hospitalDO);

            orderDO.setOrderAmountPay(orderDO.getOrderAmount());
        }
        orderDAO.updateById(orderDO);
    }
}
